<?php

/**
 * contact : 刘经理 19326416371
 * company : 版权所属 四川北星惯牌科技有限公司
 **/

namespace app\common\manager;

use app\admin\model\Team;
use app\common\model\Authentication;
use app\common\model\CaptainApplication;
use app\common\model\SignUp;
use app\common\model\User;
use think\Db;
use think\Exception;

class SignManager extends BaseManager
{

    /**
     * 添加名片
     * @param int $userId 用户uid
     * @param array $data 数据
     * @param bool $trans 是否开启事务
     * @return array
     */
    public static function createSign(int $userId,$data,bool $trans=true):array
    {
        if ($trans){
            Db::startTrans();
        }
        try {
            if (SignUp::where('user_id',$userId)->where('delete',SignUp::DELETE_STATUS_NO)->count() >= SignUp::MAX_COUNT){
                throw new Exception('您的名片已超过最大数量，无法添加');
            }
            $data['user_id'] = $userId;
            $data['create_time'] = time();
            $data['update_time'] = time();
            if (isset($data['default']) && $data['default'] == SignUp::DEFAULT_STATUS_YES){
                SignUp::where('user_id',$userId)->update(['default'=>SignUp::DEFAULT_STATUS_NO]);
            }
            $sign = SignUp::create($data);
            if ($trans)
                Db::commit();
            self::setStatusCode(self::$successCode);
            self::setMessage(self::$successMessage);
            self::setData(['id'=>$sign->id]);
        }catch (Exception $exception){
            if ($trans){
                Db::rollback();
            }
            self::setStatusCode(self::$failCode);
            self::setMessage($exception->getMessage());
        }

        return ['code'=>self::getStatusCode(),'message'=>self::getMessage(),'data'=>self::getData()];
    }

    /**
     * 修改名片
     * @param int $userId
     * @param int $id
     * @param array $data
     * @param bool $trans
     * @return array
     */
    public static function updateSign(int $userId,int $id,array $data,bool $trans=true):array
    {

        if ($trans)
            Db::startTrans();
        try {
            if (!$signInfo = SignUp::where('id',$id)->where('user_id',$userId)->find()){
                throw new Exception('当前名片信息未找到');
            }

            if (isset($data['default']) && $data['default'] == SignUp::DEFAULT_STATUS_YES){
                SignUp::where('user_id',$userId)->update(['default'=>SignUp::DEFAULT_STATUS_NO]);
            }

            if(!SignUp::where(['id'=>$id,'user_id'=>$userId])->update($data)){
                throw new Exception('名片信息更新失败');
            }
            if ($trans)
                Db::commit();
            self::setStatusCode(self::$successCode);
            self::setMessage(self::$successMessage);
        }catch (Exception $exception){
            if ($trans)
                Db::rollback();
            self::setStatusCode(self::$failCode);
            self::setMessage($exception->getMessage());
        }
        return ['code'=>self::getStatusCode(),'message'=>self::getMessage(),'data'=>self::getData()];
    }

    /**
     * 删除名片
     * @param int $userId
     * @param int $id
     * @param bool $trans
     * @return array
     */
    public static function deleteSign(int $userId,int $id,bool $trans=true):array{
        if ($trans)
            Db::startTrans();
        try {
            $data['delete'] = SignUp::DELETE_STATUS_YES;
            if (!SignUp::where(['id'=>$id,'user_id'=>$userId])->update($data)){
                throw new Exception('名片删除失败');
            }
            if ($trans)
                Db::commit();
            self::setStatusCode(self::$successCode);
            self::setMessage(self::$successMessage);
        }catch (Exception $exception){
            if ($trans)
                Db::rollback();
            self::setStatusCode(self::$failCode);
            self::setMessage(self::$failMessage);
        }
        return ['code'=>self::getStatusCode(),'message'=>self::getMessage(),'data'=>self::getData()];
    }

    /**
     * 获取个人名片
     * @param int $userId
     * @param int $page
     * @param int $step
     * @return array
     */
    public static function getSign(int $userId,int $page=1,int $step=10):array{
        $return = ['total'=>0,'totalPage'=>1,'currentPage'=>$page,'list'=>[]];
        if ($page < 1)
            $page = 1;
        try {
            $start = ($page-1)*$step;
            $count = SignUp::where(['user_id'=>$userId,'delete'=>SignUp::DELETE_STATUS_NO])->count();
            $signInfo = SignUp::where(['user_id'=>$userId,'delete'=>SignUp::DELETE_STATUS_NO])->order('default desc,id desc')->limit($start,$step)->select();
            $pageData = page($count,$step,$page);
            $return['list'] = $signInfo;
            $data = array_merge($return,$pageData);
            self::setStatusCode(self::$successCode);
            self::setMessage(self::$successMessage);
            self::setData($data);
        }catch (Exception $exception){
            self::setStatusCode(self::$failCode);
            self::setMessage($exception->getMessage());
        }
        return ['code'=>self::getStatusCode(),'message'=>self::getMessage(),'data'=>self::getData()];
    }
}